package logic;

import java.util.ArrayList;

import entities.AmmunitionCounter;
import entities.Ball;
import entities.Bat;
import entities.Wall;

/** /** BatLimiterThread class. Manages the movement limits of the bat
 *  and sets the location of the Counter and Ball to the Bat's current one.
 * @author Team 1
 *
 */
public class BatLimiterThread extends Thread{
	
	private Bat bat;						//Playground's bat.
	private ArrayList<Ball> ballList;		//Playground's current balls.
	private int width;						//Frame's width.
	boolean abort;							//The boolean that enables the thread to run or not.
	boolean aIsPressed;						//The boolean that represents either the A key is pressed or not.
	boolean dIsPressed;						//The boolean that represents either the D key is pressed or not.
	private ArrayList<Wall> wallList;		//Playground's placed walls.
	private AmmunitionCounter counter;		//The label that it's located under the bat.
	
	/** BatLimiterThread's constructor.
	 * @param bat : Playground's bat.
	 * @param width : Playground's current balls.
	 * @param wallList : Playground's placed walls.
	 * @param counter : The label that it's located under the bat.
	 * @param ballList : Playground's current balls.
	 * @param aIsPressed : The current state of the A key.
	 * @param dIsPressed : The current state of the D key.
	 */
	public BatLimiterThread(Bat bat, int width, ArrayList<Wall> wallList, ArrayList<Ball> ballList, AmmunitionCounter counter,
			Boolean aIsPressed, Boolean dIsPressed) {
		super();
		this.bat = bat;
		this.width = width;
		this.wallList = wallList;
		this.ballList = ballList;
		this.counter = counter;
		this.aIsPressed = aIsPressed.booleanValue();
		this.dIsPressed = dIsPressed.booleanValue();
	}
	
	public void run() {
		//Just runs if A key of D key is pressed.
		while((aIsPressed || dIsPressed) && !Thread.currentThread().isInterrupted()) {
			//Sets the counter label's position to the middle of the bat. (Always under it).
			counter.setLocation(Math.round(bat.getX() + (bat.getWidth() / 2)) - (counter.getWidth() / 2), counter.getY());
			if(!ballList.get(0).canMove()) {
				//If the's any ball moving at the playground, the ball's location is set in the middle of the bat.
				ballList.get(0).setLocation((bat.getX() + (bat.getWidth() / 2)) - (ballList.get(0).getWidth() / 2),
						bat.getY() - (ballList.get(0).getHeight() + 1));
			}
			if(bat.getX() + bat.getWidth()>= width - wallList.get(4).getWidth()) {
				//Sets the bat's location if it over it's right limit.
				bat.setLocation(width - wallList.get(4).getWidth() - bat.getWidth() - 1, bat.getY());
			}else if(bat.getX() <= wallList.get(3).getWidth()){
				//Sets the bat's location if it over it's left limit.
				bat.setLocation(wallList.get(3).getWidth() + 1, bat.getY());
			}
		}
		//Interrupt the thread.
		this.interrupt();
	}
	
	public void setIsApressed(boolean isApressed) {
		this.aIsPressed = isApressed;
	}
	
	public void setIsDpressed(boolean isDpressed) {
		this.dIsPressed = isDpressed;
	}
}